var express = require('express');
var path = require('path');
var fs = require('fs');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
    var pages = [];
    fs.readdir(path.join(req.app.get('views'), 'pages'), function(err, files){
	if(err)
	    res.render('index', {pages: [],
				 failed: true});
	else
	{
	    var pages = [];
	    files.forEach(function(e, i, c){
		var idx = e.lastIndexOf('.');
		if(idx != -1 && e.slice(idx + 1) == 'jade')
		    pages.push(e.slice(0, idx));
	    });
	    res.render('index', {pages: pages,
				 failed: false});
	}
    });
});

module.exports = router;
